package com.qws.plugin.presentation.flutter_presentation

import android.content.Context
import android.hardware.display.DisplayManager
import androidx.annotation.NonNull
import com.alibaba.fastjson.JSON
import com.qws.plugin.presentation.flutter_presentation.model.CustomModel
import com.qws.plugin.presentation.flutter_presentation.ui.QwsPresentation
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.embedding.engine.plugins.activity.ActivityAware
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result


/** FlutterPresentationPlugin */
class FlutterPresentationPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
    /// The MethodChannel that will the communication between Flutter and native Android
    ///
    /// This local reference serves to register the plugin with the Flutter Engine and unregister it
    /// when the Flutter Engine is detached from the Activity
    private lateinit var channel: MethodChannel
    private lateinit var binding: ActivityPluginBinding
    private var qwsPresentation: QwsPresentation? = null

    override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
        channel = MethodChannel(flutterPluginBinding.binaryMessenger, "flutter_presentation")
        channel.setMethodCallHandler(this)
    }

    override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) {
        if (call.method == "getPlatformVersion") {
            result.success("Android ${android.os.Build.VERSION.RELEASE}")
        } else if (call.method == "startSubScreen") {
            val model = JSON.parseObject(call.arguments as String, CustomModel::class.java)
            startSubScreen(model)
        } else if (call.method == "loadData") {
            val model = JSON.parseObject(call.arguments as String, CustomModel::class.java)
            qwsPresentation?.loadData(model)
        } else {
            result.notImplemented()
        }
    }

    private fun startSubScreen(model: CustomModel) {
        val mDisplayManager =
            binding.activity.getSystemService(Context.DISPLAY_SERVICE) as DisplayManager?
        val displays = mDisplayManager!!.displays
        if (displays.size > 1) {
            qwsPresentation?.dismiss()
            qwsPresentation = QwsPresentation(binding.activity, displays[1], model)
            qwsPresentation?.show()
        }
    }

    override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
        channel.setMethodCallHandler(null)
    }

    override fun onAttachedToActivity(binding: ActivityPluginBinding) {
        this.binding = binding
    }

    override fun onDetachedFromActivityForConfigChanges() {
    }

    override fun onReattachedToActivityForConfigChanges(binding: ActivityPluginBinding) {
    }

    override fun onDetachedFromActivity() {
    }
}
